package com.wwp.wkb.ui.common;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wwp.wkb.R;

import java.util.List;

public class CommonItemAdapter extends RecyclerView.Adapter<CommonItemAdapter.ViewHolder> {
    private final Context context;
    private List<CommonItem> datas;
    private OnItemClickListener onItemClickListener;

    public CommonItemAdapter(Context context, List<CommonItem> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.common_item_adapter, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CommonItem data = datas.get(position);
        holder.tv_title.setText(data.getTitle());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = getLayoutPosition();
                    CommonItem commonItem = datas.get(index);
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(v, index, commonItem);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int pos, CommonItem data);
    }
}
